<?php

namespace App\Http\Controllers\Admin\CMS;

use App\Http\Models\Admin\CMS\PostPostPros;
use App\Http\Models\Admin\CMS\PostPro;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Input;
use Psy\Util\Json;

class PostProController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        return View::make('admin.cms.postpro')->withUserinfo($request->session()->get('adminUser'))
            ->withUrltitle(trans("admin/message.postpro_index"))
            ->withAssets($request['ASSETS']);
    }
    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];
//     var_dump($datas[3]['value'],$datas[4]['value'],$length*(1+$start));
        //$posts = DB::table('PostPros')->paginate($length)->toArray();

        $posts = DB::table('post_pros')->skip($start)->take($length)->get();
        $total = DB::table('post_pros')->count();
        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($posts,$total),ResponseDto::$SUCCESS);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $validator = Validator::make(Input::all(),
            [
                'name' => 'required',
            ]);
        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{
            $PostPro = new PostPro();
            $PostPro->name = $request->input("name");
            $PostPro->desc = $request->input("desc");
            $PostPro->show_index = $request->input("show_index");
            if($request->input("id")){
                //更新操作
                $exit_item = PostPro::where('id','<>',$request->input("id"))->where('name',$PostPro->name)->first();
                if($exit_item){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                else{
                    $up_item = PostPro::find($request->input("id"));
                    $up_item->name = $PostPro->name;
                    $up_item->show_index = $PostPro->show_index;
                    $up_item->desc = $PostPro->desc;
                    $up_item->save();
                    return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
                }
            }
            else{
                //添加操作
                $find = PostPro::where('name',$request->input("name"))->first();
                if($find){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
//                var_dump(DB::insertGetId());
//                PostPro->get
                $PostPro->save();
                $insertpost = PostPro::find($PostPro->id);
                $insertpost->uid = 'property'.$PostPro->id;
                $insertpost->save();
                return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
            }

        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        if($id != -1){
            return Json::encode(PostPro::find($id));
        }else{
            $PostPros = PostPro::all();
            foreach($PostPros as $PostPro){
                $PostPro->desc = null;
                $PostPro->text = $PostPro->name;
            }
            return Json::encode($PostPros);
        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        PostPro::destroy($id);
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
}
